<?php


namespace app\index\controller;

use app\api\model\AccessTokenModel;
use app\api\model\DouYinService;
use app\api\model\Token;
use app\common\controller\Api;
use app\common\model\CateModel;
use app\common\model\ItemModel;
use app\common\model\DouyinModel;
use app\common\model\SourceModel;
use think\Controller;
use think\Exception;
use think\facade\Log;
use think\Request;
use util\Http;

class CallBack extends Controller
{
    public function douYinVerify()
    {
        if ($_SERVER['REQUEST_METHOD'] === 'POST') {
            // 获取 POST 请求的原始数据
            $postData = file_get_contents('php://input');
            $data = json_decode($postData, true);

            if($data['event'] == 'create_video'){
                try{
                    $item = ItemModel::where(['source_id' => $data['content']['share_id']])->find();
                    //获取用户ID
                    $dyItem = DouyinModel::where(['openid' => $data['from_user_id']])->find();
                    if(!$item || !$dyItem){
                        return true;
                    }
                    //更新视频状态
                    $item->dy_id = $dyItem['id'];
                    $item->item_id = $data['content']['item_id'];
                    $item->status = 1;
                    $item->save();
                    SourceModel::where(['id' => $item['sid']])->delete();
                }catch(Exception $e){
                    Log::mylog('callBack',$e->getMessage(),'test');
                    return true;
                }
            }
        } else {
            // 如果不是 POST 请求，返回错误信息
            http_response_code(405); // 返回 405 状态码表示方法不被允许
            echo 'Method Not Allowed';
        }
    }
}